package org.scrm.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.scrm.model.ResultVo;
import org.scrm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class WechatController {

    /**
     * 手机号码长度限制
     */
    private static final int MOBILE_LEN = 11;

    @Autowired
    private ProductService productService;

    @RequestMapping(path = "/mobile", method = RequestMethod.GET)
    public synchronized ResultVo sendSmsCode(@RequestParam("mobile") String mobile) throws InterruptedException {
        ResultVo resultVo = new ResultVo();
        if (!StringUtils.isNumeric(mobile) ||StringUtils.length(mobile) != MOBILE_LEN) {
            resultVo.setStatusCode("F02");
            resultVo.setErrorMsg("手机号码格式不正确");
            return resultVo;

        }
        JSONObject jsonObject = new JSONObject();
        String sended = productService.smsSend(mobile);
        jsonObject.put("sended", sended);
        if ("0".equals(sended)){
            jsonObject.put("description", "发送成功");
        }else if ("32".equals(sended)) {
            jsonObject.put("description", "同一号码发送次数太多,一天内手机号码验证码最大发送次数:5,当前次数为:5");
        }else{
            jsonObject.put("description", "发送失败");
        }
        resultVo.setData(jsonObject);
        return resultVo;
    }
}
